package com.example.clock.lockedPage;

import static com.example.clock.basicClass.Tags.getStringBigTags;
import static com.example.clock.basicClass.Tags.getStringSmallTags;
import static com.example.clock.controlClass.SqlController.addBigTag;
import static com.example.clock.controlClass.SqlController.getAllTags;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clock.R;
import com.example.clock.basicClass.Plan;
import com.example.clock.basicClass.Tags;
import com.example.clock.basicClass.Task;
import com.example.clock.controlClass.DatabaseHelper;
import com.example.clock.taskPage.TaskActivity;
import com.google.android.material.navigation.NavigationBarView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private String plan[] = new String[]{"计划1","计划2","计划3"};
    private String primary[];
    private int primaryindex;
    private String second[][] ;
    ArrayAdapter<String> adapter1,adapter2,adapter3;
    private Spinner spinner1, spinner2,spinner3;
    private Button btnSubmit;
    private Switch switchbutton;
    private TextView textView1,textView2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取所有bigtag和smalltag
        DatabaseHelper dbhelper = new DatabaseHelper(MainActivity.this,MainActivity.this);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        primary = Tags.getStringBigTags(db);
        second = Tags.getStringSmallTags(db);

        addItemsOnSpinner1();  //设置锁屏时长下拉列表
        addItemsOnSpinner2();  //设置锁屏一级事件下拉列表
        addItemsOnSpinner3();  //设置锁屏二级事件下拉列表

        //侦听锁屏一级事件，根据一级事件选择结果确定二级事件下拉框内容
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent,View view, int position, long id)
            {
                primaryindex = position;
                adapter2 = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,second[position]);
                spinner3.setAdapter(adapter2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }

        });

        addListenerOnButton(); //侦听界面“开始锁屏”按钮

    }

    public void addItemsOnSpinner1() {

        spinner1 = (Spinner) findViewById(R.id.spinner3);
        List<String> list = new ArrayList<String>();
        list.add("30");
        list.add("20");
        list.add("10");
        list.add("1");//for demo only
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(dataAdapter);
    }
    public void addItemsOnSpinner2() {

        spinner2 = (Spinner) findViewById(R.id.spinner5);

        adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, primary);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter1);
    }

    public void addItemsOnSpinner3() {

        spinner3 = (Spinner) findViewById(R.id.spinner6);
        adapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, second[0]);

        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner3.setAdapter(adapter2);
    }



    //收集界面输入数据，通过Serializable方式发送给锁屏界面，并开启锁屏界面
    public void lockstart(View view){

        spinner1 = (Spinner) findViewById(R.id.spinner3);
        spinner2 = (Spinner) findViewById(R.id.spinner5);
        spinner3 = (Spinner) findViewById(R.id.spinner6);


        //获取锁屏类型参数
        switchbutton = (Switch) findViewById(R.id.switch3);

        //获取锁屏备注参数
        textView1 = findViewById(R.id.editTextTextMultiLine);

        //获取锁屏任务名
        textView2 = findViewById(R.id.textView18);

        Task task = new Task();
        task.setPlanId("0");//自定义事件，planId默认为“0”；
        task.setLargeTags(String.valueOf(spinner2.getSelectedItem()));//获取一级事件
        task.setSmallTags(String.valueOf(spinner3.getSelectedItem()));//获取二级事件

        if(switchbutton.isChecked()){//获取锁屏类型参数值
        task.setClockedMode(2); }
        else{task.setClockedMode(1);}

        task.setPlanTime(Integer.parseInt(String.valueOf(spinner1.getSelectedItem())));//获取计划锁屏时长

        String text1 = textView1.getText().toString(); //获取锁屏备注参数字符串
        task.setTips(text1);
        String text2 = textView2.getText().toString(); //获取锁屏任务名
        task.setTaskName(text2);


        //Intent intent=new Intent(getApplicationContext(),LockActivity.class);//设置锁屏界面Intent
        Intent intent = new Intent(MainActivity.this, com.example.clock.lockedPage.LockActivity.class);
        intent.putExtra("Serializable", (Serializable) task);//设置Serializable传递数据
        startActivity(intent);//开启锁屏界面

    }

    //侦听“开始锁屏按钮，发现点击后调用lockstart
    public void addListenerOnButton() {

        btnSubmit = (Button) findViewById(R.id.button);

        btnSubmit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                lockstart(v);

            }

        });

    }

    //转到计划锁屏界面
    public void planLockScreen(View view){
        Intent intent = new Intent(MainActivity.this, com.example.clock.lockedPage.PlanLockScreen.class);
        startActivity(intent);
    }
}